<template>
	<view class="filter-wapper" v-if="value_in">
		<view class="filter-extra" @click="value_in=false"></view>
		<view class="filter-content">
			<view class="filter-box">
				<template v-if="area[0] === 1">
					<view class="filter-h4">分类</view>
					<view class="filter-con">
						<view class="filter-tags">
							<view class="filter-tag" :class="{'active': currentIndex2 === index}" v-for="(item,index) in nav2" :key="index"
							 @click="changeTab2(item,index)">
								{{item.name}}
							</view>
						</view>
					</view>
				</template>
				<template v-if="area[1] === 1">
					<view class="filter-h4">价格区间(元)</view>
					<view class="filter-con">
						<view class="filter-inputs">
							<input type="number" placeholder="最低价" v-model="price1">
							<text>——</text>
							<input type="number" placeholder="最高价" v-model="price2">
						</view>
					</view>
				</template>
				<template v-if="area[2] === 1">
					<view class="filter-h4">好评率</view>
					<view class="filter-con">
						<view class="filter-tags">
							<view class="filter-tag" :class="{'active': good === 0}" @click="selectGoods(0)">
								100%
							</view>
							<view class="filter-tag" :class="{'active': good === 1}" @click="selectGoods(1)">
								99%以上
							</view>
							<view class="filter-tag" :class="{'active': good === 2}" @click="selectGoods(2)">
								98%以上
							</view>
							<view class="filter-tag" :class="{'active': good === 3}" @click="selectGoods(3)">
								97%以上
							</view>
						</view>
					</view>
				</template>
				<template v-if="area[3] === 1">
					<view class="filter-h4">区域</view>
					<view class="filter-con">
						<view class="filter-selects">
							<view class="filter-select" @click="showPick">
								<text>{{address[0] || '请选择省'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showPick">
								<text>{{address[1] || '请选择市'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showPick">
								<text>{{address[2] || '请选择区'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</template>
			</view>
			<view class="filter-btns">
				<button plain @click="resetFilter">重置</button>
				<button plain @click="confirmFilter">确认</button>
			</view>
		</view>
		<mpvue-city-picker 
			themeColor="#007AFF" 
			ref="mpvueCityPicker" 
			:pickerValueDefault="[0, 0, 1]"
			@onConfirm="onConfirm"
		></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'; // 三级城市选择联动
	
	export default {
		components: {
			mpvueCityPicker,
		},
		props: {
			value: {
				type: Boolean,
				default: false
			},
			// 显示区域
			area: {
				type: Array,
				default() {
					return [1,1,1,1]
				}
			},
			nav2: {
				type: Array,
				default() {
					return []
				}
			},
		},
		data() {
			return {
				// #ifdef MP-WEIXIN
				wxbtn: uni.getMenuButtonBoundingClientRect(),
				// #endif
				value_in: false,
				currentIndex2: 0,
				price1: '',
				price2: '',
				good: '',
				address: [],
				categoryId: '',
			}
		},
		watch:{
			value(v) {
				this.value_in = v
			},
			value_in(v) {
				this.$emit('input',v)
			}
		},
		methods: {
			confirmFilter() {
				this.value_in = false
				this.$emit('on-confirm',{
					categoryId: this.categoryId,
					price1: this.price1,
					price2: this.price2,
					good: this.good,
					address: this.address,
				})
			},
			resetFilter() {
				this.categoryId = '';
				this.price1 = '';
				this.price2 = '';
				this.good = '';
				this.address = [];
				this.$emit('on-reset')
				this.confirmFilter()
			},
			selectGoods(v) {
				this.good = v
			},
			// 选择二级分类
			changeTab2(item,index) {
				this.currentIndex2 = index
				this.categoryId = index > 0 ? item.id : ''
			},
			// 筛选：打开省市区选择窗
			showPick() {
				this.$refs.mpvueCityPicker.show()
			},
			// 筛选：确认省市区
			onConfirm(e) {
				this.address = JSON.parse(JSON.stringify(e)).label.split('-')
			},
		},
	}
</script>

<style lang="scss" scoped>
	.filter-wapper {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: flex-end;
	}
	
	.filter-extra {
		flex-grow: 1;
		height: 100%;
	}
	
	.filter-content {
		width: 80%;
		height: 100%;
		background-color: #fff;
		box-sizing: border-box;
		padding: calc(var(--status-bar-height) + 50px) 30rpx 0;
		display: flex;
		flex-direction: column;
	}
	
	.filter-h4 {
		font-size: 30rpx;
		margin: 30rpx 0;
		
		&:nth-of-type(1) {
			margin-top: 0;
		}
	}
	
	.filter-selects {
		display: flex;
		flex-wrap: wrap;
	}
	
	.filter-select {
		background-color: #ebebeb;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #999;
		width: 200rpx;
		height: 60rpx;
		padding: 0 20rpx;
		margin: 0 20rpx 20rpx 0;
	
		image {
			width: 20rpx;
			height: auto;
		}
	}
	
	.filter-inputs {
		display: flex;
		align-items: center;
	
		input {
			background-color: #ebebeb;
			height: 80rpx;
			border-radius: 40rpx;
			text-align: center;
			font-size: 30rpx;
		}
	
		text {
			margin: 0 10rpx;
			color: #ccc;
		}
	}
	
	.filter-tags {
		display: flex;
		flex-wrap: wrap;
	}
	
	.filter-tag {
		border: 1px solid #ddd;
		font-size: 26rpx;
		padding: 5rpx 10rpx;
		flex-shrink: 0;
		margin: 0 20rpx 20rpx 0;
		border-radius: 4rpx;
	
		&.active {
			background-color: $uni-color-error;
			color: #fff;
			border: 0;
		}
	}
	
	.filter-box {
		flex-grow: 1;
	}
	
	.filter-btns {
		height: 60px;
		display: flex;
		justify-content: center;
		align-items: center;
	
		button {
			width: 200rpx;
			height: 80rpx;
			background-color: #ebebeb;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0;
			border-radius: 0;
	
			&:nth-of-type(1) {
				border-radius: 40rpx 0 0 40rpx;
			}
	
			&:nth-of-type(2) {
				border-radius: 0 40rpx 40rpx 0;
				background-color: $uni-color-error;
				color: #fff;
			}
		}
	}
</style>
